<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/utils.js"></script>
</head>

<body>
  <h1>登录</h1>
  用户名：<input type="text" id="nameInput">
  密码：<input type="password" id="pwdInput">
  <label><input type="checkbox" id="">7天免登录</label>
  <button>登录</button>
  <script>
    const nameInput = document.querySelector('#nameInput')
    const pwdInput = document.querySelector('#pwdInput')
    const freeLogin = document.querySelector('label input');
    document.querySelector('button').onclick = function () {
      var name = nameInput.value;
      var pwd = pwdInput.value;
     utils.post('../api/user/login.php',{name,pwd},resp=>{
       if(resp.code===200){
         if(freeLogin.checked){
          utils.setCookie('name',name,{path:'/',expires:7});
         }else{
           utils.setCookie('name',name,{path:'/'});
         }
         alert(resp.body.msg);
         location.replace('../index.html');
       }else{
        alert('账户名或密码错误,请重试');
        nameInput.value='';
        pwdInput.value='';
       }
     })
     

    }
  </script>
</body>

</html>